/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-10-15 18:01:58
 * Last Modified: 2016-06-13 15:48:11
 * File Name: rpc/service/service.go
 * Description:
 */
package service

import (
	"passport/account"
	"passport/avatar"
	"passport/invitecode"
	"passport/user"
)

//判断用户token是否有效
type ValidateAccessTokenReq struct {
	AccessToken string
}

type ValidateAccessTokenResp struct {
	IsValid      bool
	UserId       string
	ExpireSecond int
}

//查询用户邀请码邀请人数
type QueryInviteCodeRegNumberReq struct {
	Userid string
}

type QueryInviteCodeRegNumberResp struct {
	FriendCnt int32
}

//查询用户信息
type QueryUserInfoReq struct {
	Userid string
}

type QueryUserInfoResp struct {
	user.UserInfo
}

//查询用户信息(通过账号)
type QueryUserInfoByAccountReq struct {
	Account     string
	AccountType string
}

//RPC Service
type PassportService int

func (p *PassportService) ValidateAccessToken(req *ValidateAccessTokenReq, resp *ValidateAccessTokenResp) error {
	userid, expireSecond, ok := account.GetAccessTokenService().GetUseridByAccessToken(req.AccessToken)
	resp.IsValid = ok
	if ok {
		resp.UserId = userid
		resp.ExpireSecond = expireSecond
	}
	return nil
}

func (p *PassportService) QueryInviteCodeRegNumber(req *QueryInviteCodeRegNumberReq, resp *QueryInviteCodeRegNumberResp) error {
	resp.FriendCnt = 0
	if req.Userid == "" {
		return nil
	}
	resp.FriendCnt = invitecode.GetInviteCodeService().QueryInviteCodeRegNumber(req.Userid)
	return nil
}

func (p *PassportService) QueryUserInfo(req *QueryUserInfoReq, resp *QueryUserInfoResp) error {
	ui, uiErr := user.GetUserService().GetAllUserInfo(req.Userid)
	if uiErr != nil {
		return nil
	}
	if ui == nil {
		return nil
	}
	resp.UserInfo = *ui
	if len(ui.Avatar) == 32 { //自有的
		resp.Avatar, _ = avatar.GetAvatarURL(ui.Avatar)
	}
	return nil
}

func (p *PassportService) QueryUserInfoByAccount(req *QueryUserInfoByAccountReq, resp *QueryUserInfoResp) error {
	userid, useridErr := account.GetAccountService().GetUseridFromAccount(req.AccountType, req.Account)
	if useridErr != nil {
		return nil
	}
	r := &QueryUserInfoReq{
		Userid: userid,
	}
	return p.QueryUserInfo(r, resp)
}
